﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Peter.Sdw.Data;
using Peter.Sdw.Logic;

namespace Peter.Sdw.View
{
   /// <summary>
   /// Interaction logic for VersionView.xaml
   /// </summary>
   public partial class VersionView
   {
      private bool m_OkToUpdate;
      private TextBox m_CurrentEditBox;
      private PassageModel m_LastPassage;
      private readonly EditItemModel m_EditViewModel;

      /// <summary>
      /// Initializes a new version view.
      /// </summary>
      public VersionView ()
      {
         InitializeComponent ();
         this.m_OkToUpdate = true;
         this.m_EditViewModel = new EditItemModel ();
         this.EditView.DataContext = this.m_EditViewModel;
         this.m_EditViewModel.PropertyChanged += this.OnEditPropertyChanged;
      }

      private void OnChapterHeaderFocus (object sender, RoutedEventArgs e)
      {
         this.UpdateCurrentBox (sender as TextBox);
         this.m_EditViewModel.HasLinks = false;
         var chapter = this.TreeContents.SelectedItem as ChapterModel;
         if (chapter != null) {
            this.m_EditViewModel.Get (ItemType.Chapter, ItemType.Header, chapter.Id);
         }
      }

      private void OnChapterFooterGotFocus (object sender, RoutedEventArgs e)
      {
         this.UpdateCurrentBox (sender as TextBox);
         this.m_EditViewModel.HasLinks = true;
         var chapter = this.TreeContents.SelectedItem as ChapterModel;
         if (chapter != null) {
            this.m_EditViewModel.Get (ItemType.Chapter, ItemType.Footer, chapter.Id);
         }
      }

      private void OnPassageHeaderGotFocus (object sender, RoutedEventArgs e)
      {
         this.UpdateCurrentBox (sender as TextBox);
         this.m_EditViewModel.HasLinks = false;
      }

      private void OnPassageGotFocus (object sender, RoutedEventArgs e)
      {
         this.UpdateCurrentBox (sender as TextBox);
         this.m_EditViewModel.HasLinks = true;
         if (this.m_CurrentEditBox != null) {
            var passage = this.m_CurrentEditBox.Tag as PassageModel;
            if (passage != null) {
               this.m_LastPassage = passage;
               passage.ShowAddFooter = true;
            }
         }
      }

      private void OnPassageFooterGotFocus (object sender, RoutedEventArgs e)
      {
         this.UpdateCurrentBox (sender as TextBox);
         this.m_EditViewModel.HasLinks = true;
      }

      /// <summary>
      /// Updates the current edit text box.
      /// </summary>
      /// <param name="textBox">The text box that is currently being edited.</param>
      private void UpdateCurrentBox (TextBox textBox)
      {
         if (this.m_LastPassage != null) {
            this.m_LastPassage.ShowAddFooter = false;
            this.m_LastPassage = null;
         }
         if (this.m_CurrentEditBox != null) {
            this.m_CurrentEditBox.SelectionChanged -= this.OnTextBoxSelectionChanged;
         }
         this.m_CurrentEditBox = textBox;
         if (this.m_CurrentEditBox != null) {
            this.m_CurrentEditBox.SelectionChanged += this.OnTextBoxSelectionChanged;
         }
      }

      /// <summary>
      /// Occurs when the selection in the current text box changes;
      /// </summary>
      /// <param name="sender">The text box that raised the event.</param>
      /// <param name="e">RoutedEventArgs</param>
      private void OnTextBoxSelectionChanged (object sender, RoutedEventArgs e)
      {
         if (this.m_CurrentEditBox == null || !this.m_OkToUpdate) return;
         this.m_OkToUpdate = false;
         if (this.m_LastPassage != null) {
            this.m_LastPassage.FooterIndex = this.m_CurrentEditBox.SelectionStart;
         }
         this.m_EditViewModel.StartIndex = this.m_CurrentEditBox.SelectionStart;
         this.m_EditViewModel.EndIndex = this.m_CurrentEditBox.SelectionStart + this.m_CurrentEditBox.SelectionLength;
         this.m_OkToUpdate = true;
      }

      private void OnEditPropertyChanged (object sender, PropertyChangedEventArgs e)
      {
         if (this.m_CurrentEditBox == null || !this.m_OkToUpdate) return;
         this.m_OkToUpdate = false;
         var currLen = this.m_CurrentEditBox.Text.Length;
         var start = this.m_EditViewModel.StartIndex;
         var len = this.m_EditViewModel.EndIndex - this.m_EditViewModel.StartIndex;
         if (len < 0) len = 0;
         if (len > currLen) len = currLen;
         if (start > currLen) start = currLen;
         this.m_CurrentEditBox.Select (start, len);
         this.m_OkToUpdate = true;
      }
   }
}
